package com.serli.sample.data.runner.view;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.serli.sample.data.runner.WicketApplication;
import com.serli.sample.data.runner.domain.Trip;

public class TripEdit extends Panel {

	private static final long serialVersionUID = 1L;

	public TripEdit(String id) {
		super(id);
		setOutputMarkupId(true);
		
		Form form = new Form("tripEditForm");
		add(form);
		
		Model tripModel = new Model();
		tripModel.setObject(new Trip());
		
		{
			form.add(new TextField("editTripName", new PropertyModel(tripModel.getObject(), "name")));
			form.add(new TextField("editTripDistance", new PropertyModel(tripModel.getObject(), "distance")));
			form.add(new TextField("editTripNote", new PropertyModel(tripModel.getObject(), "note")));
		} 
		
		form.add(new ImageButton("buttonSave", tripModel) {
			@Override
			public void onSubmit() {
				System.out.println("--- onSubmit ---");
				//super.onSubmit();
				
				Trip trip = (Trip) getModelObject();
				System.out.println("Trip = "+ trip);
				((WicketApplication)getApplication()).addTrip(trip);
				setResponsePage(HomePage.class);
			}
		});
	}
}
